#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   ajrc.py
@Time    :   2024/01/26 14:08:18
@Author  :   chakcy 
@Email   :   947105045@qq.com
@description   :   吉安人才网爬虫类
'''

from pyquery import PyQuery as pq
from selenium.webdriver.common.by import By
from reptile import wait_for_element_with_id

import os
import sys
import time
sys.path.append("../../")

from reptile import Reptile
from excel import Excel

# 安吉人才爬虫类
class AJRC(Reptile):
    # 初始化
    def __init__(self, dataPath:str, driver_path):
        url = "https://www.ajrczp.com/qiuzhi/"
        list_template = ""
        sql_table = """
            create table if not exists ajrc(
                id integer,
                company varchar(50),
                phone varchar(50),
                pensonName varchar(50),
                companyScale varchar(100),
                date timestamp not null default (datetime('now', 'localtime')),
                UNIQUE (company),
                primary key (id)
                );
            """
        super().__init__(url, dataPath, list_template, sql_table, driver_path)

    # 模拟登录
    def login(self, username:str, password:str):
        self.web.get(self.url)
        print("切换登录方式")
        wait_for_element_with_id(self.web, "header")
        time.sleep(1)
        loginTypeElement = self.web.find_element(By.CSS_SELECTOR, "body > div:nth-child(3) > div.side.fl > div.member > form > div > ul.FuJia.clearfix > li:nth-child(2) > input[type=radio]")
        loginTypeElement.click()
        print("登录")
        print("输入用户名")
        usernameElement = self.web.find_element(By.CSS_SELECTOR, "body > div:nth-child(3) > div.side.fl > div.member > form > div > ul:nth-child(1) > li:nth-child(2) > input[type=text]")
        usernameElement.send_keys(username)
        passwordElement = self.web.find_element(By.CSS_SELECTOR, "body > div:nth-child(3) > div.side.fl > div.member > form > div > ul:nth-child(1) > li:nth-child(3) > input[type=password]")
        passwordElement.send_keys(password)
        print("点击登录")
        loginTypeElement = self.web.find_element(By.CSS_SELECTOR, "body > div:nth-child(3) > div.side.fl > div.member > form > div > ul:nth-child(1) > li.button > input")
        loginTypeElement.click()
        time.sleep(1)


    def getUrl(self, key:str, pages: int):
        print("搜索")
        self.key = key
        searchElement = self.web.find_element(By.XPATH, '//*[@id="header"]/div/div[2]/div[1]/div[2]/ul[1]/div/form/input[2]')
        searchElement.send_keys(key)
        btn = self.web.find_element(By.XPATH, '//*[@id="header"]/div/div[2]/div[1]/div[2]/ul[1]/div/form/input[1]')
        btn.click()
        time.sleep(1)
        self.key = self.web.current_url.split("?key=")[1]
        self.list_template = "https://www.ajrczp.com/zhaopin/net/?page={}&key=" + self.key
        self.message_url_list = []
        for page in range(1, int(pages) + 1):
            url = self.list_template.format(page)
            print(url)
            time.sleep(1)
            self.web.get(url)
            element = pq(self.web.page_source)
            self.message_url_list += self.parseUrl(element)

    def run(self):
        return super().run()
    
    def save(self):
        sql = """
        replace
        into
        ajrc (
            company,
            phone,
            pensonName,
            companyScale)
        values(?, ?, ?, ?)
        """
        return super().save(sql)
    
    def close(self):
        return super().close()
    
    def parseUrl(self, element: pq):
        message_url_list = []
        for i in element('body > div:nth-child(3) > div.content.fr > table > tbody > tr').items():
            try:
                message_url_list.append("https://www.ajrczp.com" + i.find('td a').attr('href'))
            except Exception as e:
                continue
        return message_url_list
    
    def analysis(self, element: pq):
        dataDict = {}
        dataDict['company'] = element('body > div:nth-child(3) > div.content.fr > h1').text()
        dataDict['phone'] = element('body > div:nth-child(3) > div.content.fr > table > tbody:nth-child(1) > tr:nth-child(6) > td:nth-child(2)').text()
        dataDict['pensonName'] = element('body > div:nth-child(3) > div.content.fr > table > tbody:nth-child(1) > tr:nth-child(5) > td:nth-child(2)').text()
        dataDict['companyScale'] = element('body > div:nth-child(3) > div.content.fr > table > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(4)').text()
        if len(dataDict) < 4:
            return {}
        
        return dataDict
    
class AJRCTOExcel(Excel):
    def __init__(self, path, db_name, table_name):
        super().__init__(path, db_name, table_name, 1)

    def dataToExcelAll(self):
        return super().dataToExcelAll()
    
    def dataToExcel(self):
        columns = {
            "company": "公司",
            "phone": "电话",
            "pensonName": "联系人",
            "companyScale": "公司规模",
        }
        df = self.df.rename(columns=columns)
        excel_file_path = os.path.join(self.path, f"{self.db_name.split('.')[0]}.xlsx")
        return super().excelSave(excel_file_path, df)